package cn.xm.org.controller;


import cn.xm.basic.anno.LogAnno;
import cn.xm.basic.aop.PreAuthorize;
import cn.xm.basic.util.AjaxResult;
import cn.xm.basic.util.PageList;
import cn.xm.org.domain.Employee;
import cn.xm.org.query.EmployeeQuery;
import cn.xm.org.service.EmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emp")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @PutMapping
    @ApiOperation(value = "增加或修改部门" )
    @PreAuthorize(sn = "employee:save",name = "保存")
    public AjaxResult addOrUpdate(@ApiParam(value="部门实体类",required = true) @RequestBody Employee employee) {
        if (employee.getId() == null) {
            employeeService.add(employee);
        } else {
            employeeService.update(employee);
        }
        return AjaxResult.me().success();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过ID删除" )
    @PreAuthorize(sn = "employee:delete",name = "删除")
    public AjaxResult delete(@ApiParam(value="ID属性",required = true) @PathVariable("id") Long id) {
        employeeService.delete(id);
        return AjaxResult.me().success();
    }

    @PatchMapping
    @ApiOperation(value = "批量删除" )
    @PreAuthorize(sn = "employee:batchDelete",name = "批量删除")
    public AjaxResult batchDelete(@RequestBody List<Long> ids) {
        employeeService.batchDelete(ids);
        return AjaxResult.me().success();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    @PreAuthorize(sn = "employee:findById",name = "靠id查询")
    public Employee findById(@ApiParam(value="ID属性",required = true) @PathVariable("id") Long id) {
        Employee employee = employeeService.findById(id);
        return employee;
    }

    @GetMapping("/byShopId/{id}")
    public List<Employee> findByShopId(@PathVariable("id") Long id) {
        List<Employee> employees = employeeService.findByShopId(id);
        return employees;
    }

    @GetMapping
    @ApiOperation(value = "查询所有" )
    @PreAuthorize(sn = "employee:findAll",name = "查询所有")
    public List<Employee> findAll() {
        return employeeService.findAll();
    }

    @PostMapping
    @ApiOperation(value = "分页查询" )
    @LogAnno(method = "queryByPage",methodDes = "分页查询")
    @PreAuthorize(sn = "employee:queryByPage",name = "分页查询")
    public PageList<Employee> queryByPage(@ApiParam(value="分页条件封装的类",required = true) @RequestBody EmployeeQuery employeeQuery) {
        return employeeService.queryByPage(employeeQuery);
    }


}
